package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.feign.ProductFeignService;
import com.atguigu.gulimall.ware.vo.admin.PurchaseDetailVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.PurchaseDetailDao;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;
import org.springframework.util.StringUtils;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        /*
                key:
                status:
                wareId:
         */

        LambdaQueryWrapper<PurchaseDetailEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)) {
            lambdaQueryWrapper.and((obj) -> {
                // purchaseDetial_id  sku_id
                obj.eq(PurchaseDetailEntity::getSkuId,key).or().eq(PurchaseDetailEntity::getPurchaseId,key);
            });
        }
        String status = (String) params.get("status");
        lambdaQueryWrapper.eq(!StringUtils.isEmpty(status),PurchaseDetailEntity::getStatus,status);

        String wareId = (String) params.get("wareId");
        lambdaQueryWrapper.eq(!StringUtils.isEmpty(wareId),PurchaseDetailEntity::getWareId,wareId);

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                lambdaQueryWrapper
        );

        IPage<PurchaseDetailVo> page1 = new Page<>();
        BeanUtils.copyProperties(page,page1);
        List<PurchaseDetailEntity> records = page.getRecords();
        List<PurchaseDetailVo> collect = records.stream().map((item) -> {
            PurchaseDetailVo purchaseDetailVo = new PurchaseDetailVo();
            BeanUtils.copyProperties(item, purchaseDetailVo);
            // 根据sku进行查询
            Long skuId = item.getSkuId();
//            System.out.println("skuId = " + skuId);
            R info = productFeignService.info(skuId);
            try {
                if (info.getCode() == 0) {
                    Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");

                    purchaseDetailVo.setSkuName((String) data.get("skuName"));
                }
            } catch (Exception e) {
                log.error("第一次添加采购项时候查询skuName服务调用查询skuName失败{}", e);
            }
            return purchaseDetailVo;
        }).collect(Collectors.toList());
        // 最终返回
        page1.setRecords(collect);

        return new PageUtils(page1);
    }

    @Override
    public List<PurchaseDetailEntity> listDetialBypurchaseId(Long id) {
        // 根据采购单id获取我们采购项 然后修改采购项的状态和时间
        LambdaQueryWrapper<PurchaseDetailEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PurchaseDetailEntity::getPurchaseId,id);

        // 获取我们的采购项的实例类
        List<PurchaseDetailEntity> list = list(lambdaQueryWrapper);

        // 进行返回
        return list;
    }

}